
const deviceInfo = navigator.getDeviceInfo()
console.log(JSON.stringify(deviceInfo))
// 监听玩家屏幕分辨率产生变化事件
screen.events.add('resize', () => {
    const evt = navigator.getDeviceInfo()
    scalescreen(evt.screen.height, evt.screen.width);
})

const setupScale = {
    x: 1920,
    y: 858,
}
//
const uis = UiScreen.getAllScreen()
function scalescreen(sH, sW) {
    remoteChannel.sendServerEvent({
        type: "screen",
        data: {
            screenWidth: sW,
            screenHeight: sH
        }
    })
    let widthscaleFactor = 1
    if ((sW + sH) / 2 < (setupScale.x + setupScale.y) / 2.1) {
        widthscaleFactor = (sW + sH) / (setupScale.x + setupScale.y);
    }

    uis.forEach((e) => {
        if (!e.uiScale) e.uiScale = UiScale.create();
        e.uiScale.scale = widthscaleFactor;
    })
    console.log("screen:", sW, 'x', sH);

}
//进入游戏时设置缩放一次
scalescreen(screenHeight, screenWidth)
console.log("screen:", screenWidth, 'x', screenHeight);



remoteChannel.events.on("client", (e) => {
    if(e.type == "show"){
        showBox(e.data, true)
    }
    if(e.type == "showani"){
        showBox(e.data, true,true)
        
    }
    if(e.type == "hide"){
        showBox(e.data, false)
        
    }
    if(e.type == "hideani"){
        showBox(e.data, false, true)
        
    }
})

/** 仅支持首层 */
async function showBox(name, boo, ani) {
    const box = ui.findChildByName(name)
    if (box) {
        if (ani) {
            const uiScale = UiScale.create()
            box.uiScale = uiScale
            uiScale.scale = boo ? 0 : 1
            box.visible = true
            if (!box.setupOpacity)
                box.setupOpacity = box.backgroundOpacity
            function setupOpacity(box) {
                for (let j of box.children) {
                    // if (j.children.length > 0) setupOpacity(j)
                    if (!j.setupOpacity)
                        j.setupOpacity = j.backgroundOpacity

                }
            }
            function setOpacity(box) {
                box.children.forEach(async (j) => {
                    // if (j.children.length > 0) setOpacity(j)
                    for (let i = 0; i < j.setupOpacity; i += 0.1) {
                        j.backgroundOpacity = boo ? i : j.setupOpacity - i
                        await sleep(1000 / 60)
                    }
                })

            }
            setupOpacity(box)
            setOpacity(box);
            (async () => {
                for (let i = 0; i < box.setupOpacity; i += 0.1) {
                    box.backgroundOpacity = boo ? i : box.setupOpacity - i
                    await sleep(1000 / 60)
                }
            })()


            for (let i = 0; i < 1; i += 0.1) {
                uiScale.scale = boo ? i : 1 - i;
                await sleep(1000 / 60)
            }
            uiScale.scale = boo ? 1 : 0;
        }
        box.visible = boo
    } else return console.warn(`showBox: can't find box "${name}"`)
    return true
}